package com.woniuxy.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Map;

public class UserContext {
    public static  Integer ContextUserId() throws ParseException {
        //从请求上下问获取Request对象
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String jwt = request.getHeader("authorization");
        Map<String, Object> userMap = JWTUtil.getUserMap(jwt);
        return ((Long) userMap.get("id")).intValue();

    }
}
